package org.utils.ui;

import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.StringTokenizer;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;

import org.utils.common.InitException;
import org.utils.xml.XMLDataNode;

public class CustomMenuBar extends JMenuBar
{
	private static final long serialVersionUID = -379960835218784431L;
	//	private JFrame parent = null;
	private ArrayList items = null;

	public CustomMenuBar(JFrame pParent, XMLDataNode config) throws InitException
	{
//		parent = pParent;
		items = new ArrayList();
		init(config);
	}

	protected void init(XMLDataNode config) throws InitException
	{
	   try
	   {
		ArrayList childs = config.getChildren();
		JMenu menuItem = null;
		for (int i = 0; i < childs.size(); i++)
		{
			XMLDataNode node = (XMLDataNode) childs.get(i);
//			Hashtable atts = node.getAttributes();
			String name = node.getAttribute("name");
			menuItem = new JMenu( name );
			if (node.getAttribute("mnemonic") != null)
				menuItem.setMnemonic( node.getAttribute("mnemonic").charAt(0) );
			initMenuItems(menuItem, node.getChildren());
			add(menuItem);
		}
	   }
	   catch (NullPointerException e)
	   {
	   	throw new InitException("Problem with the menu bar xml configuration", e);
	   }
	}

	protected void setAccelerator(JMenuItem item, String accelerator) throws InitException
	{
		Class eventClass = java.awt.Event.class;
		Class keyEventClass = java.awt.event.KeyEvent.class;
		StringTokenizer tokens = new StringTokenizer(accelerator, "+");
		int modifiers = 0;
		int keyCode = 0;
		try
		{
			while (tokens.hasMoreTokens())
			{
				String token = tokens.nextToken();
				if (token.indexOf("MASK") > 0)
					modifiers |= eventClass.getField(token).getInt(null);
				else
					keyCode = keyEventClass.getField("VK_"+token).getInt(null);
			}
		}
		catch (NoSuchFieldException e)
		{
			throw new InitException("L'un des accelerateurs pour le MenuITem \""+item.getText()+"\" est introuvable", e);
		}
		catch (IllegalAccessException e)
		{
			throw new InitException("L'un des accelerateurs pour le MenuITem \""+item.getText()+"\" est introuvable", e);
		}
		item.setAccelerator(KeyStroke.getKeyStroke(keyCode, modifiers));
	}

	protected void initMenuItems(JMenu menuItem, ArrayList childs) throws InitException
	{
		JMenuItem item = null;
		for (int i = 0; i < childs.size(); i++)
		{
			XMLDataNode node = (XMLDataNode) childs.get(i);
			String name = node.getAttribute("name");
			if (node.hasChildren())
			{
				item = new JMenu( name );
				initMenuItems( (JMenu) item, node.getChildren());
			}
			else
				item = new JMenuItem( name );
			if (node.getAttribute("accelerator") != null)
				setAccelerator(item, node.getAttribute("accelerator"));
			if (node.getAttribute("mnemonic") != null)
				item.setMnemonic( node.getAttribute("mnemonic").charAt(0) );
			if (node.getAttribute("actioncommand") != null)
				item.setActionCommand( node.getAttribute("actioncommand") );
			items.add(item);
			menuItem.add(item);
		}
	}

	public void addActionListener(ActionListener listener)
	{
		for (int i = 0; i < items.size(); i++)
		{
			((JMenuItem) items.get(i)).addActionListener(listener);
		}
	}
}
